package com.danskin.wechat.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danskin.wechat.dao.CustInfoExDao;
import com.danskin.wechat.dao.CustPointRecordDao;
import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.CustInfoExView;
import com.danskin.wechat.entity.CustPointRecord;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.service.CustPointRecordService;
import com.danskin.wechat.service.WechatMsgTemplateService;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;

@Service
public class CustPointRecordServiceImpl extends BaseServiceImpl implements CustPointRecordService {
	@Autowired
	private CustPointRecordDao custPointRecordDao;
	@Autowired
	private CustInfoExDao custInfoExDao;
	@Autowired
	private WechatMsgTemplateService msgTemplateService;

	@Override
	public int saveCustPointRecord(CustPointRecord entity, WxMediaPlatform mp) {
		CustInfoExView custInfoEx = custInfoExDao.selectCustByMemberId(mp.getId(), entity.getMemberId());
		if (custInfoEx != null) {
			entity.setCustExId(custInfoEx.getId());

			if (entity.getAvaPoints() != null) {

				custInfoEx.setAvaPoints(entity.getAvaPoints());
			}
			if (entity.getPoints() != null && entity.getPoints() > 0) {
				custInfoEx.setTotalPoints(custInfoEx.getTotalPoints() + entity.getPoints());
			}

			int insertCount = 0;
			try {
				//保存会员可用积分、总积分
				custInfoExDao.updateByPrimaryKeySelective2(custInfoEx);

				//保存会员积分明细
				insertCount = custPointRecordDao.insertSelective(entity);

				// 发送积分变动微信消息通知
				if (insertCount > 0) {
					msgTemplateService.sendCustChangePointsMsg(mp.getAccessToken(), mp.getAppId(),
							custInfoEx.getWxOpenId(), custInfoEx.getName(), custInfoEx.getMobile(), entity.getPoints(),
							custInfoEx.getAvaPoints());
				}
			} catch (Exception e) {
				LogUtil.error("========新增用户积分记录异常", e);
			}

			return insertCount;
		}
		return -1;
	}

	@Override
	public List<CustPointRecord> findCustPointRecordListAPI(Page page, String type) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
		String dt = fmt.format(new Date());
		Map<String, Object> map = page.getMap();
		if (type.equals("usable")) {
			map.put("isUsed", 0);
		} else if (type.equals("used")) {
			map.put("isUsed", 1);
		} else if (type.equals("overtime")) {
			map.put("expiredTime", dt);
		}
		return custPointRecordDao.listPageCustPointRecordListAPI(page);
	}

	@Override
	public int insertCustPointRecord(CustPointRecord param) {
		LogUtil.info("========新建积分领取记录参数：" + ToStringBuilder.reflectionToString(param));

		int insertCount = 0;

		try {
			insertCount = custPointRecordDao.insertCustPointRecord(param);
		} catch (Exception e) {
			LogUtil.error("========新建积分领取记录异常", e);
		}
		return insertCount;
	}

}
